#include <QtGui> 
#include <QMessageBox>
#include <stdio.h>
#include <iostream>
#include "tinytimerapp.h"

using namespace::std;
// including <QtGui> saves us to include every class user, <QString>, <QFileDialog>,...

TinyTimerApp::TinyTimerApp(QWidget *parent)
{
	setupUi(this); // this sets up GUI


	// signals/slots mechanism in action
	connect( pushButton_start, SIGNAL( clicked() ), this, SLOT( startCountDown() ) ); 
	connect( pushButton_pause, SIGNAL( clicked() ), this, SLOT( pauseCountDown() ) );
	connect( pushButton_restart, SIGNAL( clicked() ), this, SLOT( restartCountDown() ) );
	connect( &cdthread, SIGNAL( changeTimer(const QTime &)),timeEdit,SLOT(setTime(const QTime &)));
	connect( &cdthread, SIGNAL( changeTimer(const QTime &)),this,SLOT(changeWinTitle(const QTime &)));
	connect( &cdthread, SIGNAL( timeUp()),this,SLOT(timeUpMessageBox()));

	//set MessageBox
	//timeup=QMessageBox(QMessageBox::Information,"Info","Time is up!!!",QMessageBox::Ok);
}

void TinyTimerApp::startCountDown(){
	//Set the initial time to timeEdit
	//QTime initTime(0,1,10);
	//timeEdit->setTime(initTime);

	if(pushButton_start->text()=="Start"){
		pushButton_start->setText("Stop");
	}else{
		pushButton_start->setText("Start");
	}
	QTime initTime=timeEdit->time();
	//cout<<"startCountDown after set Init Time.........."<<endl;
	cdthread.startcountdown(initTime);

}

void TinyTimerApp::pauseCountDown(){

	if(pushButton_pause->text()=="Pause"){
		pushButton_pause->setText("Continue");
	}else{
		pushButton_pause->setText("Pause");
	}
	cdthread.pausecountdown();
}

void TinyTimerApp::restartCountDown(){
	cdthread.restartcountdown();	
}

void TinyTimerApp::timeUpMessageBox(){
	//timeup.exec();
	pushButton_start->setText("Start");
	QMessageBox::information(this,"info","Time is up!!!");
}

void TinyTimerApp::changeWinTitle(const QTime & now){
	QString title;
	QString temp;
	temp.setNum(now.hour());
	if(now.hour()<10){
		title.append("0");
	}
	title.append(temp);
	title.append(":");
	temp.setNum(now.minute());
	if(now.minute()<10){
		title.append("0");
	}
	title.append(temp);
	title.append(":");
	temp.setNum(now.second());
	if(now.second()<10){
		title.append("0");
	}

	title.append(temp);
	//cerr<<now.hour()<<":"<<now.minute()<<":"<<now.second()<<endl;
	//cerr<<title.toStdString()<<endl;
	this->setWindowTitle(title);
	
	//this->setWindowTitle("tttttt");

}
